<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>歌手管理</el-breadcrumb-item>
      <el-breadcrumb-item>歌手类型列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-form ref="ruleForm" :rules="rules" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="歌手类型" prop="singerType">
          <el-input v-model="formInline.singerType" placeholder="请输入歌手类型"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="onSubmit('ruleForm')">添加歌手类型</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="类型名">
          <template slot-scope="scope">
            <span>{{scope.row.singertype_name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{scope.row.singertype_create_time}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(scope.row.singertypeid)"></el-button>
            <el-button size="mini" icon="el-icon-setting" type="primary" @click="handleSinger(scope.row.singertypeid)">歌手管理</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="编辑歌手类型" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="ruleForm2">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSingerType, addSingerType, deleteSingerType, editSingerType } from '../../utils/api'
export default {
  data: function () {
    return {
      // 表格数据
      tableData: [],
      // 添加歌手类型的表单的验证
      // 修改歌手类型的表单的验证
      rules: {
        singerType: [
          { required: true, message: '请输入歌手类型', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入歌手类型', trigger: 'blur' }
        ],
      },
      // 添加歌手类型的表单
      formInline: {
        singerType: ''
      },
      // 控制修改歌手类型表单的显示
      dialogFormVisible: false,
      // 编辑歌手类型的表单
      form: {
        name: '',
        id: 0
      }
    }
  },
  created () {
    this.getSingerTypeL();
  },
  methods: {
    // 编辑歌手类型 准备数据
    handleEdit (scope) {
      this.form.id = scope.singertypeid;
      this.form.name = scope.singertype_name;
      this.dialogFormVisible = true;
    },
    // 删除歌手类型
    handleDelete (id) {
      this.$confirm('此操作将永久删除该歌手类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await deleteSingerType(id)
        if (res.status == 0) {
          this.$message({
            message: '删除歌手类型成功！',
            type: 'success'
          });
          this.getSingerTypeL();
        } else {
          this.$message.error(res.message);
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSinger (id) {
      this.$router.push(`/singerlist?singertypeid=${id}`);
    },
    // 获取歌手类型
    async getSingerTypeL () {
      let res = await getSingerType();
      if (res.status == 0) {
        this.tableData = res.data;
      } else {
        this.$message.error('获取歌手类型列表失败！');
      }
    },
    // 添加歌手类型
    onSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await addSingerType(this.formInline.singerType, new Date().toLocaleString());
          if (res.status == 0) {
            this.$message({
              message: '添加歌手类型成功！',
              type: 'success'
            });
            this.getSingerTypeL();
            this.formInline.singerType = '';
          } else {
            this.$message.error(res.message);
          }
        }
      });
    },
    // 编辑歌手类型
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await editSingerType(this.form.id, this.form.name);
          if (res.status == 0) {
            this.$message({
              message: '修改歌手类型成功！',
              type: 'success'
            });
            this.getSingerTypeL();
            this.form.id = 0;
            this.form.name = '';
          } else {
            this.$message.error(res.message);
          }
          this.dialogFormVisible = false;
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>

</style>